home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / spawn.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  6KB  |  172 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. """distutils.spawn
  5.  
  6. Provides the 'spawn()' function, a front-end to various platform-
  7. specific functions for launching another program in a sub-process.
  8. Also provides the 'find_executable()' to search the path for a given
  9. executable name.
  10. """
  11. __revision__ = '$Id: spawn.py 37828 2004-11-10 22:23:15Z loewis $'
  12. import sys
  13. import os
  14. import string
  15. from distutils.errors import *
  16. from distutils import log
  17.  
  18. def spawn(cmd, search_path = 1, verbose = 0, dry_run = 0):
  19.     """Run another program, specified as a command list 'cmd', in a new
  20.     process.  'cmd' is just the argument list for the new process, ie.
  21.     cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
  22.     There is no way to run a program with a name different from that of its
  23.     executable.
  24.  
  25.     If 'search_path' is true (the default), the system's executable
  26.     search path will be used to find the program; otherwise, cmd[0]
  27.     must be the exact path to the executable.  If 'dry_run' is true,
  28.     the command will not actually be run.
  29.  
  30.     Raise DistutilsExecError if running the program fails in any way; just
  31.     return on success.
  32.     """
  33.     if os.name == 'posix':
  34.         _spawn_posix(cmd, search_path, dry_run = dry_run)
  35.     elif os.name == 'nt':
  36.         _spawn_nt(cmd, search_path, dry_run = dry_run)
  37.     elif os.name == 'os2':
  38.         _spawn_os2(cmd, search_path, dry_run = dry_run)
  39.     else:
  40.         raise DistutilsPlatformError, "don't know how to spawn programs on platform '%s'" % os.name
  41.     return os.name == 'posix'
  42.  
  43.  
  44. def _nt_quote_args(args):
  45.     '''Quote command-line arguments for DOS/Windows conventions: just
  46.     wraps every argument which contains blanks in double quotes, and
  47.     returns a new argument list.
  48.     '''
  49.     for i in range(len(args)):
  50.         if string.find(args[i], ' ') != -1:
  51.             args[i] = '"%s"' % args[i]
  52.             continue
  53.     
  54.     return args
  55.  
  56.  
  57. def _spawn_nt(cmd, search_path = 1, verbose = 0, dry_run = 0):
  58.     executable = cmd[0]
  59.     cmd = _nt_quote_args(cmd)
  60.     if search_path:
  61.         if not find_executable(executable):
  62.             pass
  63.         executable = executable
  64.     
  65.     log.info(string.join([
  66.         executable] + cmd[1:], ' '))
  67.     if not dry_run:
  68.         
  69.         try:
  70.             rc = os.spawnv(os.P_WAIT, executable, cmd)
  71.         except OSError:
  72.             exc = None
  73.             raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  74.  
  75.         if rc != 0:
  76.             raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc)
  77.         rc != 0
  78.     
  79.  
  80.  
  81. def _spawn_os2(cmd, search_path = 1, verbose = 0, dry_run = 0):
  82.     executable = cmd[0]
  83.     if search_path:
  84.         if not find_executable(executable):
  85.             pass
  86.         executable = executable
  87.     
  88.     log.info(string.join([
  89.         executable] + cmd[1:], ' '))
  90.     if not dry_run:
  91.         
  92.         try:
  93.             rc = os.spawnv(os.P_WAIT, executable, cmd)
  94.         except OSError:
  95.             exc = None
  96.             raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  97.  
  98.         if rc != 0:
  99.             print "command '%s' failed with exit status %d" % (cmd[0], rc)
  100.             raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], rc)
  101.         rc != 0
  102.     
  103.  
  104.  
  105. def _spawn_posix(cmd, search_path = 1, verbose = 0, dry_run = 0):
  106.     log.info(string.join(cmd, ' '))
  107.     exec_fn = dry_run if dry_run else os.execv
  108.     pid = os.fork()
  109.     if pid == 0:
  110.         
  111.         try:
  112.             exec_fn(cmd[0], cmd)
  113.         except OSError:
  114.             e = None
  115.             sys.stderr.write('unable to execute %s: %s\n' % (cmd[0], e.strerror))
  116.             os._exit(1)
  117.  
  118.         sys.stderr.write('unable to execute %s for unknown reasons' % cmd[0])
  119.         os._exit(1)
  120.     else:
  121.         while None:
  122.             
  123.             try:
  124.                 (pid, status) = os.waitpid(pid, 0)
  125.             except OSError:
  126.                 exc = None
  127.                 import errno as errno
  128.                 if exc.errno == errno.EINTR:
  129.                     continue
  130.                 
  131.                 raise DistutilsExecError, "command '%s' failed: %s" % (cmd[0], exc[-1])
  132.  
  133.             if os.WIFSIGNALED(status):
  134.                 raise DistutilsExecError, "command '%s' terminated by signal %d" % (cmd[0], os.WTERMSIG(status))
  135.             if os.WIFEXITED(status):
  136.                 exit_status = os.WEXITSTATUS(status)
  137.                 if exit_status == 0:
  138.                     return None
  139.                 raise DistutilsExecError, "command '%s' failed with exit status %d" % (cmd[0], exit_status)
  140.             os.WIFEXITED(status)
  141.             if os.WIFSTOPPED(status):
  142.                 continue
  143.                 continue
  144.             raise DistutilsExecError, "unknown error executing '%s': termination status %d" % (cmd[0], status)
  145.             continue
  146.             return None
  147.  
  148.  
  149. def find_executable(executable, path = None):
  150.     """Try to find 'executable' in the directories listed in 'path' (a
  151.     string listing directories separated by 'os.pathsep'; defaults to
  152.     os.environ['PATH']).  Returns the complete filename or None if not
  153.     found.
  154.     """
  155.     if path is None:
  156.         path = os.environ['PATH']
  157.     
  158.     paths = string.split(path, os.pathsep)
  159.     (base, ext) = os.path.splitext(executable)
  160.     if (sys.platform == 'win32' or os.name == 'os2') and ext != '.exe':
  161.         executable = executable + '.exe'
  162.     
  163.     if not os.path.isfile(executable):
  164.         for p in paths:
  165.             f = os.path.join(p, executable)
  166.             if os.path.isfile(f):
  167.                 return f
  168.         
  169.         return None
  170.     return executable
  171.  
  172.